<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@page import="org.acegisecurity.context.SecurityContextHolder"%>
<%@page import="org.acegisecurity.userdetails.UserDetails"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Sign</title>
</head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<script language="JavaScript">
	var xmlHttp;
	
	function createXMLHttpRequest(){
		if(window.ActiveXObject){			
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else if(window.XMLHttpRequest){
			xmlHttp = new XMLHttpRequest();
		}
	}
	
	function doSearch(){
			var url = "../AjaxServlet?" + new Date().getTime();
			
			createXMLHttpRequest();
			xmlHttp.onreadystatechange = handleStateChange;
			xmlHttp.open("GET", url, true);
			xmlHttp.send(null);
	} 
	
	function handleStateChange(){
		if(xmlHttp.readyState==4){
			if(xmlHttp.status == 200){
				parseSearchResults();
			}
			else{
				alert("Error access AjaxServlet!");
			}
		}
	}
	
	function parseSearchResults(){
		var resultDiv = document.getElementById("message_label");
		while(resultDiv.childNodes.length > 0){
			resultDiv.removeChild(resultDiv.childNodes[0]);
		}
		
		var allResults = xmlHttp.responseXML.getElementsByTagName("news");
		var result = null;
		for(var i = 0; i < allResults.length; i++){
			result = allResults[i];
			parseResult(result, resultDiv);
		}
	}
	
	function parseResult(result, resultDiv){
		var count = (getChildElementText(result, 'count'));
		if(count == 0){
		    var news_info = document.createElement('font');	    
		    news_info.appendChild(document.createTextNode('No new messages.'));
		    resultDiv.appendChild(news_info);
		}else{
		    var news_info = document.createElement("a");
		    news_info.setAttribute("href","../message/message_inbox.action");
		    var news_info_text = document.createElement('font');
		    news_info_text.setAttribute('color','white');
		    news_info_text.appendChild(document.createTextNode('You have ' + count + ' New Messages.'));
		   	var news_info_img = document.createElement('img');
		   	news_info_img.setAttribute('src','../images/new-message.gif');
		   	news_info_img.setAttribute('border','0');
		   	news_info.appendChild(news_info_img);
			news_info.appendChild(news_info_text);
			resultDiv.appendChild(news_info);
		}
		
	}
	
	function getChildElementText(parentNode, childTagName){
		var childTag = parentNode.getElementsByTagName(childTagName);
		return childTag[0].firstChild.nodeValue;
	}
	
	function logout(){
		if (confirm("Are You Sure You Want to Log Out?"))
		top.location = "../j_acegi_logout";
		return false;
	}
	
	function init(){
		var begin;
		begin = setInterval("doSearch()",5000000);
	}
</script>
<meta http-equiv="refresh" content="60">
<base target="main">
<link href="../images/skin.css" rel="stylesheet" type="text/css">
<body onload="doSearch();">   
	<script>init();</script> 
	<table width="100%" height="64" border="0" cellpadding="0" cellspacing="0" class="admin_topbg">
	  <tr>
	    <td width="61%" height="64"><img src="../images/logo.gif" width="262" height="64"></td>
	    <td width="39%" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
	      <tr>
	        <td width="74%" height="38" class="admin_txt"><b></b> Hello <%
	        	Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	        	if (obj instanceof UserDetails) {
	        		UserDetails userDetails = (UserDetails) obj;
	        		out.print(userDetails.getUsername());
	        	}
	        %>,  <label id="message_label">You have 0 new messages.</label> </td>
	        <td width="22%"><a href="#" target="_self" onClick="logout();"><img src="../images/out.gif" alt="Safely Logonout" width="46" height="20" border="0"></a></td>
	        <td width="4%">&nbsp;</td>
	      </tr>
	      <tr>
	        <td height="19" colspan="3">&nbsp;</td>
	        </tr>
	    </table></td>
	  </tr>
	</table>
</body>
</html>